import $ from '../common';
Page({
  data: {
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
    isIphoneX: $.gData().isIphoneX,
    imgs:[],
    noimg:$.noImage
  },
  onLoad(e) {
    wx.getSystemInfo({
      success: res => {
        let winWidth = res.windowWidth
        let otherWidth = Math.floor((winWidth - 50) / 3)
        this.setData({
          otherWidth
        })
      },
    })
    this.setData({
      zonecode: wx.getStorageSync('addresscode') || '',
      uuid:e.uuid,
      testpaper_uuid:e.testpaper_uuid,
      user:$.parent().data.user
    })
    this.getgroups()
  },
  onShow() {

  },
  backHome(){//返回
    $.navigateBack()
  },
  getgroups(){//获取数据信息
    $.get('/testpaper/student/detail',{
      zonecode:this.data.zonecode,
      studentpaper_uuid:this.data.uuid
    },res=>{
      this.setData({
        info:res
      })
    })
  },
  addImg() {
    if (this.data.imgs.length == 9) {
      $.tip('最多只能上传9张图片！')
      return
    }
    this.updataImg()
  },
  updataImg(index, cb) {//选择图片
    wx.chooseImage({
      count: parseInt(9 - this.data.imgs.length),
      sizeType: "compressed",
      success: (res) => {
        let newList = res.tempFilePaths
        $.uploadFileList('/image/upload/oss', {
            fileList: newList,
            prefix: "keday/"
          },
          (res) => {
            this.setData({
              'imgs': this.data.imgs.concat(res)
            })
          },
          (err) => {
            console.log('上传失败:', err)
          }
        )
      }
    })
  },
  textinputhand(e){
    const {value}=e.detail
    this.setData({note:value})
  },
  sumbit(){
    const{note='',imgs=[],uuid,testpaper_uuid}=this.data
    $.post('/testpaper/student/submit',{
      memo:note,
      imgs:imgs,
      studentpaper_uuid:uuid,
      testpaper_uuid:testpaper_uuid
    },res=>{
      $.parent().reset()
      $.navigateBack()
    })
  }
})